package com.faxsun.core.persist.dao.impl;

import java.util.ArrayList;
import java.util.List;

import mblog.core.utils.BeanMapUtils;
import mtons.modules.persist.impl.DaoImpl;
import mtons.modules.pojos.UserProfile;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.transaction.annotation.Transactional;

import com.faxsun.core.persist.dao.BlacklistDao;
import com.faxsun.core.persist.entity.BlacklistPO;

public class LiveBlacklistDaoImpl extends DaoImpl<BlacklistPO> implements
		BlacklistDao {
	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory
			.getLogger(LiveBlacklistDaoImpl.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = -2355015131690745040L;

	protected LiveBlacklistDaoImpl() {
		super(BlacklistPO.class);
	}

	@Override
	@Caching(evict = {
			@CacheEvict(value = "blacklistCache", key = "'ids'+new String(#liveId)", beforeInvocation = true),
			@CacheEvict(value = "blacklistCache", key = "'profile'+new String(#liveId)", beforeInvocation = true) })
	public void remove(long userId, long liveId) {
		BlacklistPO entity = super
				.findUnique("from BlacklistPO where user.id=? and liveId=?",
						userId, liveId);
		if (entity == null)
			return;
		super.delete(entity);
	}

	@Override
	@Cacheable(value = "blacklistCache", key = "'ids'+new String(#liveId)", condition = "#ids!=null&&#ids.size()>0")
	@Transactional
	public List<Long> getChannelDisabledBlackist(long liveId) {
		List<BlacklistPO> list = super.findBy("liveId", liveId);
		List<Long> ids = new ArrayList<Long>();
		for (BlacklistPO o : list) {
			ids.add(o.getUser().getId());
		}

		return ids;
	}

	@Override
	@Cacheable(value = "blacklistCache", key = "'profile'+new String(#liveId)")
	@Transactional
	public List<UserProfile> getChannelDisabledUserList(long liveId) {
		List<BlacklistPO> list = super.findBy("liveId", liveId);
		List<UserProfile> users = new ArrayList<UserProfile>();
		for (BlacklistPO item : list) {
			users.add(BeanMapUtils.copyPassport(item.getUser()));
		}
		return users;
	}

}
